package edu.gdkm.service.impl;

import edu.gdkm.service.UserinfoService;
import edu.gdkm.sql.dao.OrderDetailMapper;
import edu.gdkm.sql.dao.OrderMapper;
import edu.gdkm.sql.dao.ProductMapper;
import edu.gdkm.sql.dao.UserinfoMapper;
import edu.gdkm.sql.pojo.Order;
import edu.gdkm.sql.pojo.OrderDetail;
import edu.gdkm.sql.pojo.Product;
import edu.gdkm.sql.pojo.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserinfoServiceImpl implements UserinfoService {
   @Autowired
    UserinfoMapper userinfoMapper;
   @Autowired
    OrderMapper orderMapper;
   @Autowired
    OrderDetailMapper orderDetailMapper;
   @Autowired
    ProductMapper productMapper;

    @Override
    public String showOrders(String username) {
        Userinfo userinfo = userinfoMapper.getUserinfoByUsername(username);
        System.out.println(userinfo);
        List<Order> orders=orderMapper.getOrdersByUserId(userinfo.getId());
        for (Order order : orders) {
            System.out.println(order);
            List<OrderDetail> orderDetails=orderDetailMapper.getOrderDetailsByOrderId(order.getId());
            for (OrderDetail orderDetail : orderDetails) {
                System.out.print(orderDetail);
                Product product=productMapper.findById(orderDetail.getProductid());
                System.out.println("，"+product);
            }
        }
        return "";
    }

    @Override
    public Userinfo login(String username, String password) {
        Userinfo userinfosql=userinfoMapper.getUserinfoByUsername(username);
        if(userinfosql!=null&&userinfosql.getPassword().equals(password)){
            return userinfosql;
        }else {
            return null;
        }
    }

    @Override
    public boolean changePassword(Userinfo userinfo, String newpass) {
        Userinfo userinfosql=userinfoMapper.getUserinfoByUsername(userinfo.getUsername());
        if (userinfosql!=null){
            if (userinfosql.getPassword().equals(userinfo.getPassword())){
                userinfosql.setPassword(newpass);
                userinfoMapper.updateUserinfo(userinfosql);
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public Userinfo register(Userinfo userinfo) {
        Userinfo userinfosql=userinfoMapper.getUserinfoByUsername(userinfo.getUsername());
        if(userinfosql==null){
            userinfoMapper.insertUserinfo(userinfo);
            return userinfoMapper.getUserinfoByUsername(userinfo.getUsername());
        }else {
            return null;
        }
    }
}
